roles = function() {
	return {
		/*--------------------------------------窗体定义--------------------------------------------*/
		panel: function() {
			//角色STORE
			var rolestore = new Store({
				fields: ['id', 'name', 'organ', 'remark'],
				proxy: {
			        type: 'ajax',
			        url: '../admin/role.jspx?index',
			        reader: {
			            type: 'json',
			            root: 'datas',
			            totalProperty: 'total'
			        }
			    },
			    autoLoad: false,
			    listeners: {
			    	load: function() {
			    		var autosm = Ext.getCmp('roleautogrid').getSelectionModel();
			    		autosm.deselectAll();
			    	}
			    }
			});
			
			//默认加载数据
			rolestore.removeAll();
			rolestore.currentPage = 1;
			rolestore.proxy.extraParams = {name: '', organ: '', inside: true};
			rolestore.load();
			
			//角色列表
			var pageToolBar = Ext.create('Ext.PagingToolbar', {
	            store: rolestore,
	            draggable : false,
	            displayInfo: true
	        });
			var rolegrid = new Grid({
				title: '前台角色列表',
				id: 'rolegrid',
				region: 'center',
				store: rolestore,
				minColumnWidth: 35,
				stripeRows: true,
				bbar : pageToolBar,
				tbar: [{
					text: '刷新',
					scale: 'medium',
					iconCls: 'icon-refresh',
					handler: function() {
						roles.refreshRoleAction();
					}
				}],
				columns: [
		             new Ext.grid.RowNumberer(),
		             {header: '序号', dataIndex: 'id', hidden: true},
		             {header: '名称', dataIndex: 'name', width: 200},
		             {header: '所属组织', dataIndex: 'organ', width: 250},
		             {header: '备注说明', dataIndex: 'remark', width: 250}
				],
				listeners: {
					select: function(fn, rec, index, eOpts) {
						roles.clickgridRoleAction(rec);
					}
				}
			});
			//权限STORE
			var autostore = new Store({
				fields: ['id', 'name'],
				proxy: {
			        type: 'ajax',
			        url: '../admin/role.jspx?indexOFaction'
			    },
			    autoLoad: true
			});
			
			//选择组件
			var rolesm = Ext.create('Ext.selection.CheckboxModel', {
				mode: 'SIMPLE'
			});
			
			var autogrid = new Grid({
				id: 'roleautogrid',
		        store: autostore,
		        region: 'east',
		        width: '28%',
		        title: '权限列表',
		        selModel: rolesm,
		        tbar: [{
					text: '刷新',
					scale: 'medium',
					iconCls: 'icon-refresh',
					handler: function() {
						roles.refreshAutoAction();
					}
				}, "-", {
					text: '保存权限',
					id: 'roleSave',
					disabled: true,
					scale: 'medium',
					iconCls: 'icon-config',
					handler: function() {
						roles.saveRoleAction();
					}
				}],
				columns: [
		             new Ext.grid.RowNumberer(),
		             {header: '序号', dataIndex: 'id', hidden: true},
		             {header: '权限名称', dataIndex: 'name', flex: 1, renderer: function(v) {
		            	 return "<span class='actionCls'>" + v + "</span>";
		             }}
				]
			});
			
			var panel = new Panel({
				layout: 'border',
				border: false,
				items: [rolegrid, autogrid]
			});
			
			return panel;
		},
				
		/*--------------------------------------触发事件定义--------------------------------------*/
		//刷新角色事件
		refreshRoleAction: function() {
			Ext.getCmp('rolegrid').getStore().load();
		},
		//选择角色时触发的动作
		clickgridRoleAction: function(rec) {
			var res = RequestByUrl('../admin/role.jspx?indexOFauto', {
				id: rec.get('id')
			});
			var autostore = Ext.getCmp('roleautogrid').getStore();
			var autosels = Ext.getCmp('roleautogrid').getSelectionModel();
    		var autos = res.msg.split(";");
			var ook = [];
			for (var i = 0; i < autos.length - 1; i++) {
				ook.push(autostore.getById(parseInt(autos[i])));
			}
			autosels.select(ook);
		},
		//保存角色权限事件
		saveRoleAction: function() {
			var sel = Ext.getCmp('rolegrid').getSelectionModel().getSelection();
			if (sel.length < 1) {
				Ext.Msg.alert('提示', '请选择要保存权限的角色！');
			} else {
				var autosels = Ext.getCmp('roleautogrid').getSelectionModel().getSelection();
				var autostr = "";
				for (var i = 0; i < autosels.length; i++) {
					autostr = autostr + autosels[i].get('id') + ";";
				}
				var res = RequestByUrl('../admin/role.jspx?save', {
					id: sel[0].get('id'),
					autostr: autostr
				});
				if (res.success) {
					Ext.Show.msg(res.msg, '');
				}
			}
		},
		//刷新权限
		refreshAutoAction: function() {
			Ext.getCmp('roleautogrid').getStore().load();
			Ext.getCmp('rolegrid').getSelectionModel().deselectAll();
		}
	};
}();